package com.ruoyi.charge.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.enums.CommissionType;
import com.ruoyi.common.enums.DeletedStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.ruoyi.charge.bo.CashProportionAddBo;
import com.ruoyi.charge.bo.CashProportionQueryBo;
import com.ruoyi.charge.bo.CashProportionEditBo;
import com.ruoyi.charge.domain.CashProportion;
import com.ruoyi.charge.mapper.CashProportionMapper;
import com.ruoyi.charge.vo.CashProportionVo;
import com.ruoyi.charge.service.ICashProportionService;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 分佣比例管理Service业务层处理
 *
 * @author fanqie
 * @date 2021-10-25
 */
@Service
public class CashProportionServiceImpl extends ServiceImpl<CashProportionMapper, CashProportion> implements ICashProportionService {

    @Autowired
    private CashProportionMapper cashProportionMapper;

    @Override
    public CashProportionVo queryById(Long id) {
        CashProportion db = this.baseMapper.selectById(id);
        return BeanUtil.toBean(db, CashProportionVo.class);
    }

    @Override
    public List<CashProportionVo> queryList(CashProportionQueryBo bo) {
        LambdaQueryWrapper<CashProportion> lqw = Wrappers.lambdaQuery();
        return entity2Vo(this.list(lqw));
    }

    /**
     * 实体类转化成视图对象
     *
     * @param collection 实体类集合
     * @return
     */
    private List<CashProportionVo> entity2Vo(Collection<CashProportion> collection) {
        List<CashProportionVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, CashProportionVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<CashProportion> page = (Page<CashProportion>) collection;
            Page<CashProportionVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page, pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(CashProportionAddBo bo) {
        CashProportion add = BeanUtil.toBean(bo, CashProportion.class);
        validEntityBeforeSave(add);
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(CashProportionEditBo bo) {
        CashProportion update = BeanUtil.toBean(bo, CashProportion.class);
        validEntityBeforeSave(update);
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(CashProportion entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return this.removeByIds(ids);
    }

    @Override
    public BigDecimal getStoreProportion() {

        CashProportion proportion = this.getByType(CommissionType.STORE.getCode());
        if (proportion != null) {
            return proportion.getProportion();
        }
        return null;
    }

    @Override
    public BigDecimal getDistrictProportion() {
        CashProportion proportion = this.getByType(CommissionType.DISTRICT.getCode());
        if (proportion != null) {
            return proportion.getProportion();
        }
        return null;
    }

    @Override
    public BigDecimal getCityProportion() {
        CashProportion proportion = this.getByType(CommissionType.CITY.getCode());
        if (proportion != null) {
            return proportion.getProportion();
        }
        return null;
    }

    @Override
    public BigDecimal getProvinceProportion() {
        CashProportion proportion = this.getByType(CommissionType.PROVINCE.getCode());
        if (proportion != null) {
            return proportion.getProportion();
        }
        return null;
    }

    private CashProportion getByType(Integer type) {
        CashProportion cashProportion = cashProportionMapper.selectOne(
                new LambdaQueryWrapper<CashProportion>()
                        .eq(CashProportion::getType, type)
                        .eq(CashProportion::getDeleted, DeletedStatus.VALID.getCode())
                        .last("limit 1")
        );

        return cashProportion;

    }
}
